export async function openDatabase(dbName) {
  console.log(`[openDatabase] 尝试打开数据库: ${dbName}`);
  return new Promise((resolve, reject) => {
    try {
      const dbPath = `_doc/${dbName}`;
      // 检查数据库是否已打开
      if (plus.sqlite.isOpenDatabase({ name: dbName, path: dbPath })) {
        console.log(`✅ [openDatabase] 数据库 "${dbName}" 已经打开`);
        resolve(true);
        return;
      }

      // 打开数据库
      plus.sqlite.openDatabase({
        name: dbName,
        path: dbPath,
        success: () => {
          console.log(`✅ [openDatabase] 数据库 "${dbName}" 打开成功`);
          resolve(true);
        },
        fail: (error) => {
          console.error(`❌ [openDatabase] 数据库 "${dbName}" 打开失败:`, error);
          reject(new Error(`[openDatabase] 数据库打开失败: ${error.message}`));
        },
      });
    } catch (error) {
      console.error('❌ [openDatabase] 数据库初始化失败:', error);
      reject(new Error(`[openDatabase] 数据库初始化失败: ${error.message}`));
    }
  });
}

export async function closeDatabase(dbName) {
  console.log(`[closeDatabase] 尝试关闭数据库: ${dbName}`);
  return new Promise((resolve, reject) => {
    const dbPath = `_doc/${dbName}`;
    // 检查数据库是否已关闭
    if (!plus.sqlite.isOpenDatabase({ name: dbName, path: dbPath })) {
      console.log(`✅ [closeDatabase] 数据库 "${dbName}" 已经关闭`);
      resolve(true);
      return;
    }

    // 关闭数据库
    plus.sqlite.closeDatabase({
      name: dbName,
      success: () => {
        console.log(`✅ [closeDatabase] 数据库 "${dbName}" 已成功关闭`);
        resolve(true);
      },
      fail: (error) => {
        console.error(`❌ [closeDatabase] 数据库 "${dbName}" 关闭失败:`, error);
        reject(new Error(`[closeDatabase] 数据库关闭失败: ${error.message}`));
      },
    });
  });
}

// // export async function initDatabase(dbName) {
//   console.log(`[initDatabase] 初始化数据库表结构: ${dbName}`);
//   return new Promise((resolve, reject) => {
//     const createTableSQL = `
//       CREATE TABLE IF NOT EXISTS tibetan_dict (
//         id INTEGER PRIMARY KEY AUTOINCREMENT,
//         "序号" TEXT DEFAULT NULL, 
//         "卷པོད།" TEXT DEFAULT NULL, 
//         "སྡེ།" TEXT DEFAULT NULL,
//         "部" TEXT DEFAULT NULL, 
//         "སྐོར།" TEXT DEFAULT NULL, 
//         "类" TEXT DEFAULT NULL,
//         "则ཚན་པ།" TEXT DEFAULT NULL, 
//         "满" TEXT DEFAULT NULL, 
//         "藏" TEXT DEFAULT NULL,
//         "词性" TEXT DEFAULT NULL, 
//         "藏文词性标注" TEXT DEFAULT NULL, 
//         "句法规则" TEXT DEFAULT NULL,
//         "拉丁撰写" TEXT DEFAULT NULL, 
//         "蒙古文拉丁" TEXT DEFAULT NULL, 
//         "蒙" TEXT DEFAULT NULL,
//         "维" TEXT DEFAULT NULL, 
//         "汉" TEXT DEFAULT NULL, 
//         "简体字" TEXT DEFAULT NULL,
//         "日" TEXT DEFAULT NULL, 
//         "词性标注" TEXT DEFAULT NULL
//       );
//     `;
//     plus.sqlite.executeSql({
//       name: dbName,
//       sql: createTableSQL,
//       success: () => {
//         console.log(`✅ [initDatabase] 表结构初始化成功`);
//         resolve(true);
//       },
//       fail: (error) => {
//         console.error(`❌ [initDatabase] 表结构初始化失败:`, error);
//         reject(new Error(`[initDatabase] 表结构初始化失败: ${error.message}`));
//       },
//     });
//   });
// }